package controller
{
	import flash.events.GeolocationEvent;
	import flash.events.StatusEvent;
	import flash.sensors.Geolocation;

	public class GPSController
	{
		public var coord:Object;
		private var geoLocation:Geolocation;
		
		public function GPSController()
		{
			coord = {lat:39.111948, lon:-77.080078};
			locate();
		}
		
		public function locate():void {
			if(Geolocation.isSupported){
				trace('[GPSController]: Geolocation supported.');
				
				geoLocation = new Geolocation();
				if ( !geoLocation.muted ) {
					trace('[GPSController]: Adding listeners.');
					geoLocation.setRequestedUpdateInterval(10000);
					geoLocation.addEventListener(GeolocationEvent.UPDATE, handleLocationRequest);
					geoLocation.addEventListener(StatusEvent.STATUS, handleStatus);
				} else {
					trace('[GPSController]: Geolocation muted.');
				}
				
			} else {
				trace('[GPSController]: Geolocation not supported.');
				// do nothing as the coord object was already defaulted 
			}
		}
		
		private function handleStatus(event:StatusEvent):void {
			trace('[GPSController]: Status event.');
		}
		
		private function handleLocationRequest(event:GeolocationEvent):void { 
			trace('[GPSController]: Geolocation location received.');
			coord.lat = event.latitude;
			coord.lon = event.longitude;
		}
	}
}